require( '../../../../config' );
var vpnDao = require( '../../../../../app/components/backend/db').vpn,
    vpnModel = require('../../../../../app/components/backend/db/models/vpn'),
    assert = require('assert'),
    helper = require('../../../../helper'),
    testData1 = {
      host: '127.56.121.230',
      pwd: 'jjGHYIIU8Cbbrt'
    },
    testData2 = {
      host: '164.56.21.220',
      pwd: '4fGHYIIJFEbbrt'
    };

describe('app/components/backend/db/daos/vpn', function() {
  // 测试后置处理
  afterEach(function() {

    // 复位
    vpnModel.remove({}, function (err) {
      if (err) throw new Error(err);
      Promise.resolve();
    });
  });
  describe('#findOneAndUpdate()', function () {
    it('[vpn.findOneAndUpdate.Nor.01].创建/更新vpn', function () {

      // 插入一条
      var findPromise = vpnDao.findOneAndUpdate({host: testData1.host}, testData1);
      helper.isBluebirdPromise(findPromise);
      return findPromise.then(function (result1) {
        assert.equal(result1.host, testData1.host);
        assert.equal(result1.pwd, testData1.pwd);

        // 再插入一条
      }).then(vpnDao.findOneAndUpdate.bind(vpnDao, {host: testData2.host}, testData2)).then(function (result2) {
        assert.equal(result2.host, testData2.host);
        assert.equal(result2.pwd, testData2.pwd);
        testData1.pwd = 'xxxxxxxxxx';

        // 更新一条
      }).then(vpnDao.findOneAndUpdate.bind(vpnDao, {host: testData1.host}, testData1)).then(function (result3) {
        assert.equal(result3.host, testData1.host);
        assert.equal(result3.pwd, testData1.pwd);
      });
    });
  });
});